package org.sundatagroup.aurora.core.controller;


import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.sundatagroup.aurora.core.entity.pojo.Module;
import org.sundatagroup.aurora.core.entity.pojo.Project;
import org.sundatagroup.aurora.core.service.ModuleService;
import org.sundatagroup.aurora.core.service.ProjectService;

import java.util.List;

/**
 * <p>各项目或模块的查询相关
 * </p>
 * 
 * @version 0.2.0
 * <br>只保留查询项目相关的、返回 Json 格式数据的请求控制
 * 
 * @author lui
 * @author Zdream
 * @date 2018-05-25
 * @since 0.1
 */
@RequestMapping("/aurora")
@Controller
public class ModuleController implements IJsonResultController {
	
	@Autowired
	ProjectService projectService;
	
	@Autowired
	ModuleService moduleService;

	/**
	 * 根据 pid 查询对应 project 包含的 module
	 * @param pid
	 *   project id 项目编号
	 * @return
	 *   返回项目下所有模块
	 */
	@RequestMapping(value = "/show/do/queryModulesOfProject",
			method = RequestMethod.GET,
			produces = {"application/json"})
	@ResponseBody
	public String queryModulesOfProject(@RequestParam int pid){
		Project p = projectService.queryByPid(pid);
		JsonObject json = (p == null) ? rawResultJsonObject(false) : rawModulesJson(p.getModules());
		
		return json.toString();
	}

	/**
	 * 根据 pid 查询对应 project 的所有数据.
	 * @param pid
	 *   project id 项目编号
	 * @return
	 *   以 Json 字符串形式返回 project 数据
	 * @since 0.1.1
	 */
	@RequestMapping(value = "/show/do/queryProject",
			method = RequestMethod.GET,
			produces = {"application/json"})
	@ResponseBody
	public String queryProject(@RequestParam int pid){
		Project p = projectService.queryByPid(pid);
		JsonObject json = (p == null) ? rawResultJsonObject(false) : rawModulesJson(p.toJson());
		
		return json.toString();
	}

	/**
	 * 分页查找模块.
	 * @param start
	 *   分页起始位置, 默认 0
	 * @param size
	 *   分页大小, 默认 10
	 * @return
	 */
	@RequestMapping(value = "/show/do/queryModules",
			method = RequestMethod.GET,
			produces = {"application/json"})
	@ResponseBody
	public String queryModules(
			@RequestParam(value = "start", defaultValue = "0") int start,
			@RequestParam(value = "size", defaultValue = "10") int size){
		JsonObject json = rawModulesJson(moduleService.queryByPage(start, size));
		return json.toString();
	}

	/**
	 * 查询 module 的总数
	 */
	@RequestMapping(value = "/show/do/countModules",
			method = RequestMethod.GET,
			produces = {"application/json"})
	@ResponseBody
	public String countModules(){
		JsonObject json = rawResultJsonObject(true);
		json.addProperty("count", moduleService.countModules());

		return json.toString();
	}

	/**
	 * 查询 project 的总数
	 */
	@RequestMapping(value = "/show/do/countProjects",
			method = RequestMethod.GET,
			produces = {"application/json"})
	@ResponseBody
	public String countProjects(){
		JsonObject json = rawResultJsonObject(true);
		json.addProperty("count", projectService.countProjects());

		return json.toString();
	}
	
	/**
	 * 返回预设的 json 数据, 用于发送给客户端
	 * @return
	 */
	private JsonObject rawModulesJson(List<Module> modules) {
		JsonObject root = new JsonObject();
		root.addProperty("result", true);
		
		JsonArray array = new JsonArray();
		modules.forEach((m) -> {
			array.add(m.toJson());
		});
		
		root.add("datas", array);
		return root;
	}
	
	/**
	 * 将 JsonObject 数据封装后返回, 用于发送给客户端
	 * @return
	 */
	private JsonObject rawModulesJson(JsonObject o) {
		JsonObject root = rawResultJsonObject();
		root.add("datas", o);
		return root;
	}
}
